import { requestWithErrorHandling } from "./request";

// 根据用户ID查询评论（分页）
export const getCommentsByUserId = (userId, params = {}) => {
  return requestWithErrorHandling({
    url: `/comments/user/${userId}`,
    method: "GET",
    params: {
      page: params.page || 1,
      size: params.size || 10,
    },
    errorMessage: "获取用户评论失败",
  });
};

// 根据文章ID查询评论（分页）
export const getCommentsByArticleId = (articleId, params = {}) => {
  return requestWithErrorHandling({
    url: `/comments/article/${articleId}`,
    method: "GET",
    params: {
      page: params.page || 1,
      size: params.size || 10,
      sortWay: params.sortWay || "create_time", // 新增排序参数，默认按创建时间
    },
    errorMessage: "获取文章评论失败",
  });
};

// 查询评论（分页）
export const getComments = (params) => {
  return requestWithErrorHandling({
    url: "/comments",
    method: "GET",
    params: {
      page: params.page || 1,
      size: params.size || 10,
      username: params.username || "",
      articleTitle: params.articleTitle || "",
      content: params.content || "",
    },
    errorMessage: "获取评论列表失败",
  });
};

// 根据id查询评论
export const getCommentById = (id) => {
  return requestWithErrorHandling({
    url: `/comments/${id}`,
    method: "GET",
    errorMessage: "获取评论详情失败",
  });
};

// 新增评论
export const createComment = (data) => {
  return requestWithErrorHandling({
    url: "/comments",
    method: "POST",
    data: {
      content: data.content,
      star: parseFloat(data.star), // 确保是浮点数
      articleTitle: data.articleTitle,
      username: data.username,
    },
    errorMessage: "创建评论失败",
  });
};

// 修改评论
export const updateComment = (data) => {
  return requestWithErrorHandling({
    url: "/comments",
    method: "PUT",
    data: {
      id: data.id,
      content: data.content,
      star: parseFloat(data.star), // 确保是浮点数
      articleTitle: data.articleTitle,
      username: data.username,
    },
    errorMessage: "修改评论失败",
  });
};

// 删除评论
export const deleteComment = (id) => {
  return requestWithErrorHandling({
    url: `/comments/${id}`,
    method: "DELETE",
    errorMessage: "删除评论失败",
  });
};

// 批量删除评论
export const batchDeleteComments = (ids) => {
  return requestWithErrorHandling({
    url: `/comments/batch/${ids.join(",")}`,
    method: "DELETE",
    errorMessage: "批量删除评论失败",
  });
};

// 获取AI评论
export const getAiCommentsByArticleId = (articleId) => {
  return requestWithErrorHandling({
    url: `/comments/article/ai/${articleId}`,
    method: "GET",
    errorMessage: "获取AI评论失败",
  });
};

// 生成AI评论
export const generateAiComments = (articleId) => {
  return requestWithErrorHandling({
    url: `/generate/ai_comment/${articleId}`,
    method: "POST",
    timeout: 60000, // 设置60秒超时,因为AI生成需要较长时间
    errorMessage: "生成AI评论失败",
  });
};
